package com.firewolf.mybatis;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.firewolf.mybatis.beans.User2;

public class MainDriver {
	@Test
	public void testAlias() throws IOException {
		Reader reader = Resources.getResourceAsReader("conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);

		SqlSession session = factory.openSession();

		String statement = "userMapper.qryUserById";
		User user = session.selectOne(statement, 1);
		System.out.println(user);

		String statement2 = "userMapper.qryUser2ById";
		User2 user2 = session.selectOne(statement2, 1);
		System.out.println(user2);

		session.close();
	}

	/**
	 	1.加载SqlSessionFactory，可以指定环境名
	 	2.可以有多个环境，每一个环境对应一个SqlSessionFactory
	    3.如果没有写的话，会加载默认的
	 * @throws IOException
	 */
	@Test
	public void testEnvironment() throws IOException {
		Reader reader = Resources.getResourceAsReader("conf.xml");
		//指定要使用的环境
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,"hibernate");

		SqlSession session = factory.openSession();

		String statement = "userMapper.qryUserById";
		User user = session.selectOne(statement, 1);
		System.out.println(user);

		String statement2 = "userMapper.qryUser2ById";
		User2 user2 = session.selectOne(statement2, 1);
		System.out.println(user2);

		session.close();
	}
}
